package nl.haemers.managers 
{
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.utils.Dictionary;
	
	import nl.haemers.managers.SoundObject;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	/**
	 * Class Description:
	 * - Easy registersounds
	 * - Option to preload sounds (not the responsibility of the soundmanager. It manage sounds it doesn't load them)
	 * - Easy to unregistersounds
	 * - Handle error
	 * - Handle progress of the (pre)loading (not the responsibility of the soundmanager. It manage sounds it doesn't load them)
	 * - Easy to play sound
	 * - Easy to look up sounds
	 * 
	 * 
	 */
	public class SoundManager
	{
		private var _sounds				:Dictionary;
		
		private var _multiplay			:Boolean;
		private var _mute				:Boolean;
		
		private var _playingSounds		:Array;
		
		private var _soundChannel		:SoundChannel;
		private var _soundTransform		:SoundTransform;
		
		private var _soundObject		:SoundObject;
		
		private var _volume 			:Number;
		
		public function SoundManager( multiplay:Boolean = true ) 
		{
			/*The user is responsible for unregistering the sounds!*/
			_sounds = new Dictionary( false );
			_multiplay = multiplay;
		}
		/** Public methods **/
		public function registerSound( sound:Sound, key:String, soundTransform:SoundTransform = null ) : Boolean
		{
			if (_sounds[key]) return false;
			
			_soundObject = new SoundObject(sound, soundTransform);
			_soundObject.key = key;
			_sounds[key] = _soundObject;
			return true;
		}
		public function unregisterSound( key:String ) : Boolean
		{
			if (!_sounds[key]) return false;
			delete _sounds[key];
			return true;
		}
		public function playSound( key:String ) : void
		{
			trace(key + " : " + _sounds[key]);
			_soundObject = _sounds[key];
			_soundObject.play();
		}
		public function pauseSound( key:String ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.pause();
		}
		public function stopSound( key:String ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.stop();
		}
		public function resetSound( key:String ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.reset();
		}
		
		public function getSoundTransform( key:String ) : SoundTransform
		{
			_soundObject = _sounds[key];
			return _soundObject.soundTransform;
		}
		public function addSoundTransform( key:String, soundTransform:SoundTransform ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.soundTransform = soundTransform;
		}
		public function getSoundChannel( key:String ) : SoundChannel
		{
			_soundObject = _sounds[key];
			return _soundObject.soundChannel;
		}		
		
		public function setVolume( key:String, volume:Number ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.volume = volume;
		}
		public function setPan( key:String, pan:Number ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.pan = pan;
		}
		public function setPosition( key:String, position:Number ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.position = position;
		}
		public function setInfinityLoop( key:String ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.infinityLoop = true;
		}
		public function unsetInfinityLoop( key:String ) : void
		{
			_soundObject = _sounds[key];
			_soundObject.infinityLoop = false;
		}
		
		public function getSound( key:String ) : Sound
		{
			_soundObject = _sounds[key];
			if (_soundObject)return _soundObject.sound;
			return null;
		}
		
		/** Getters & Setters **/
		public function get keys() : Array
		{
			var _keys:Array = new Array();
			var i:int = 0;
			for each(_soundObject in _sounds)
			{
				_keys[i++] = _soundObject.key;
			}
			
			return _keys;
		}
		
		public function get volume() : Number
		{ 
			return _volume; 
		}
		public function set volume( value:Number ) : void
		{
			var key:String;
			_volume = value;
			for each(key in _sounds)
			{
				_soundObject = _sounds[keys];
				_soundObject.volume = _volume;
			}
		}
		
		public function get mute() : Boolean
		{ 
			return _mute; 
		}
		public function set mute( value:Boolean ) : void 
		{
			if (_mute != value)
			{
				_mute = value;
				var key:SoundObject;
				for each(key in _sounds)
				{
					key.mute = _mute;
				}
			}
		}
		
		public function get playingSounds() : Array
		{
			var i:int = 0;
			_playingSounds = new Array();
			for each(_soundObject in _sounds)
			{
				if (_soundObject.playing)
				{
					_playingSounds[i++] = _soundObject.key;
				}
			}
			return _playingSounds;
		}
		/** UseFull? **/
		/*
		public function get multiplay() : Boolean
		{ 
			return _multiplay; 
		}
		public function set multiplay( value:Boolean ) : void 
		{
			_multiplay = value;	
		}
		//*/
		/*
		public function get soundMixTween() : 
		{ 
			return _soundMixTween; 
		}
		public function set soundMixTween( value: ) : void
		{
			_soundMixTween = value;
		}
		*/
		
		/** Protected methods **/
		
		/** Private methods **/
		
	}
}